/*
编写 : Hexh
时间 : 2012-5-7 10:29:53
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsBcar
	/// </summary>
	public class FsBcarController : BaseController
	{


		private readonly IFsBcarFacade FsBcarFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public FsBcarController(IFsBcarFacade FsBcarFacade, IHospUserFacade HospUserFacade)
		{
			this.FsBcarFacade = FsBcarFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问FsBcar管理页面")]
		[ViewPage]
		public ActionResult FsBcar()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改FsBcar信息")]
		public ActionResult SaveOrUpdate()
		{
			FsBcar entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsBcar
				{
				// ForeignKey Set
					//Bcarid = new HashedSet<FsBcarPermission>(), 
				// ForeignKey Set
					//Bcarid = new HashedSet<FsBcarsend>(), 
					IdCard = FsBcarFacade.Load(Request["m_idcard"]),
					Type = Request["m_type"], 
					CarNo = Request["m_carno"], 
					Owner = Request["m_owner"], 
					//Deptid = IDepartmentFacade.Load(Request["m_deptid"]),
					Department = Request["m_department"], 
					Description = Request["m_description"], 
					State = Request["m_state"]==null ? 0 : int.Parse(Request["m_state"]), 
				};
			}
			else
			{
				// get the entity.
				entity = FsBcarFacade.Get(Request["Id"]);
				// set new value..
					// ForeignKey Set
				//FsBcarPermissionList = new HashedSet<FsBcarPermission>(); 
					// ForeignKey Set
				//FsBcarsendList = new HashedSet<FsBcarsend>(); 
				//entity.m_idcard = Request["m_idcard"]==null ? null : FsBcar.Parse(Request["m_idcard"]); 
				//entity.m_type = Request["m_type"]; 
				//entity.m_carno = Request["m_carno"]; 
				//entity.m_owner = Request["m_owner"]; 
				//entity.m_deptid = Request["m_deptid"]==null ? null : InfraDepartment.Parse(Request["m_deptid"]); 
				//entity.m_department = Request["m_department"]; 
				//entity.m_description = Request["m_description"]; 
				//entity.m_state = Request["m_state"]==null ? 0 : int.Parse(Request["m_state"]); 
			}
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				//entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			// 保存信息
			FsBcarFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除FsBcar基本资料")]
		public ActionResult Delete()
		{
			FsBcarFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsBcar基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsBcarFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsBcar清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsBcarFacade.GetPlist(
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			return this.JsonFormat(
				new ExtGirdData<FsBcar> { total = total, data = data },
				new string[] { "CustomType" });
		}
		
		[Description("获取FsBcar信息")]
		public ActionResult GetById()
		{
			var entity = FsBcarFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsBcar> { data = new FsBcar[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsBcar字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsBcarFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
